<?php
namespace app\admin\controller;

use think\Db;
use think\Controller;

class User extends Controller
{
    public function add()
    {
        $school = Db::name("school")->select();
        // dump($role);
        //把变量保存在模板变量中
        $this->assign("school", $school);
        return $this->fetch();
    }

    //完成添加用户功能

    public function doadduser()
    {
        $sNumber = input('sNumber');
        //判断学号是否存在
        $num = Db::name("user")->where("sNumber", $sNumber)->count();
        if ($num > 0)
            $this->error("学号已存在，请更换邮箱。或联系客服。");
        $data = [
            "name" => input('username'),
            "teleNumber" => input('phoneNum'),
            "sNumber" => $sNumber,
            "password" => md5(input('password')),
            "adddate" => date('Y-m-d H:i:s'),
            "status" => 0,
            "schoolId" => input('schoolId')
        ];
        dump($data);
        $res = Db::name('user')->insert($data);
        if ($res) {
            $this->success("添加成功", 'userlist');
        } else {
            $this->error("添加失败", 'userlist');
        }
    }

    //显示用户列表
    public function userlist()
    {
        if (isset($_POST["uid"])) {
            $uid = $_POST["uid"];
            $user = Db::name('user u')->join("school s", "u.schoolId=s.schoolId")->where("uid", $uid)
                ->select();
            $this->assign("usersa", $user);
            return "succuess";
        } else {
            $user = Db::name('user u')->join("school s", "u.schoolId=s.schoolId")->limit(0,1)
                ->select();
        }
        $all = Db::name('user u')->join("school s", "u.schoolId=s.schoolId")->count();
        $this->assign("all", $all);
        $this->assign("usersa", $user);
        return $this->fetch();
    }

    //test

    public function users()
    {
        return $this->fetch("users");
    }

    //修改用户
    public function edituser($uid)
    {
        //用户信息
        //$user=Db::name('user')->where('uid',$uid)->select();
        $user = Db::name('user')->find($uid); //find 返回一维数组 一条记录   //select 返回二维数组  多条记录
        //dump($user);
        $this->assign('user', $user);
        //角色列表
        $school = Db::name('school')->select();
        $this->assign('school', $school);
        return $this->fetch();
    }

    //修改用户成功
    public function updateuser()
    {
        $uid = input('uid');
        $date = [
            "name" => input('username'),
            "teleNumber" => input('phoneNum'),
            "sNumber" => input('sNumber'),
            "schoolId" => input('schoolId')
        ];
        $res = Db::name("user")->where("userId", $uid)->update($date);
        if ($res) {
            $this->success("修改成功", 'userlist');
        } else {
            $this->error("修改失败", 'userlist');
        }
    }

    //重置密码
    public function resetpassword($uid)
    {
        $password = Db::name('user')->find($uid);
        $this->assign('users', $password);
        return $this->fetch();
    }

    //重置密码为11
    public function resetpw()
    {
        $uid = input('uid');
        $data = [
            "password" => md5("11")
        ];
        $res = Db::name("user")->where("userId", $uid)->update($data);
        // var_dump($uid);
        // var_dump($res);
        if ($res) {
            $this->success("重置成功", 'userlist');
        } else {
            $this->error("重置失败", 'userlist');
        }
    }


    //删除用户
    public function deluser($uid)
    {
        if (Db::name('user')->delete($uid)) {
            $this->success('删除成功', 'userlist');
        } else {
            $this->error('删除失败', 'userlist');
        }
    }

    public function status()
    {
        $status = input('status');
        $uid = input('uid');

        $res = Db::name('user')->where("userId", $uid)->update([
            'status' => $status,
        ]);
        if ($res)
            echo "success";
        else
            echo "fail";
    }

    // 根据数据库来限制条数
    public function limit()
    {
        $pageNum = Db::name('user u')->join("school s", "u.schoolId=s.schoolId")->count();
        $lenth = input('lenth');
        $res = Db::name('user u')->join("school s", "u.schoolId=s.schoolId")
            ->limit(0, $lenth)->select();
        echo json_encode($res);
    }
    //根据ajax请求来查询数据库
    public function search()
    {
        $key = input('key');
        // $limit = input('limit');
        $res = Db::name('user u')->join("school s", "u.schoolId=s.schoolId")
            ->whereLike('name', '%' . $key . '%')->select();
        $num = Db::name('user u')->join("school s", "u.schoolId=s.schoolId")
            ->whereLike('name', '%' . $key . '%')->count();
            // echo Db::name('user')->getLastSql();
        // dump($key);
        $this->assign("res",$res);
        $this->assign("num",$num);
        // echo json_encode($res);
        return $this->fetch();
    }

    // 分页器
    public function pageselector(){
        $lenth = input('lenth');
        $page = input('page');    
        $offset = $lenth*($page-1);
        $res = Db::name('user u')->join("school s", "u.schoolId=s.schoolId")
        ->limit($offset, $lenth)->select();
        echo json_encode($res);
    }

}
